package xin.marcher.module.comment.api.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import xin.marcher.module.comment.api.SkuWaitCommentApi;
import xin.marcher.module.comment.domain.dto.SkuWaitCommentDTO;
import xin.marcher.module.comment.domain.dto.SkuWaitCommentResultDTO;
import xin.marcher.module.comment.domain.request.QuerySkuWaitCommentPageRequest;
import xin.marcher.module.comment.domain.request.SkuWaitCommentRequst;
import xin.marcher.module.comment.service.SkuWaitCommentService;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.exception.ProductBizException;

import javax.annotation.Resource;

/**
 * 商品待评价Api
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = SkuWaitCommentApi.class, retries = 0)
public class SkuWaitCommentApiImpl implements SkuWaitCommentApi {

    @Resource
    private SkuWaitCommentService skuWaitCommentService;

    /**
     * 保存待评价信息接口
     */
    @Override
    public JsonResult<SkuWaitCommentResultDTO> saveWaitComment(SkuWaitCommentRequst request) {
        try {
            SkuWaitCommentResultDTO result = skuWaitCommentService.saveWaitComment(request);
            return JsonResult.buildSuccess(result);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 查询用户的商品待评价列表接口
     */
    @Override
    public JsonResult<PageResult<SkuWaitCommentDTO>> listWaitCommentPage(QuerySkuWaitCommentPageRequest request) {
        try {
            PageResult<SkuWaitCommentDTO> result = skuWaitCommentService.listWaitCommentPage(request);
            return JsonResult.buildSuccess(result);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

}
